import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { SupplierContractEntity } from '../../entity/contract';
import { SupplierContractService } from '../../service/contract';
import { SupplierSupplierEntity } from '../../entity/supplier';

/**
 * 供应商合同
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: SupplierContractEntity,
  service: SupplierContractService,
  pageQueryOp: {
    fieldEq: ['a.type', 'a.status'],
    keyWordLikeFields: ['a.name'],
    where: async ctx => {
      const { startDate, endDate } = ctx.request.body;
      const where = [];
      if (startDate) {
        where.push(['a.startDate >= :startDate', { startDate }]);
      }
      if (endDate) {
        where.push(['a.endDate <= :endDate', { endDate }]);
      }
      return where;
    },
    select: ['a.*', 'b.name as supplierName'],
    join: [
      {
        entity: SupplierSupplierEntity,
        alias: 'b',
        condition: 'a.supplierId = b.id',
        type: 'leftJoin',
      },
    ],
  },
})
export class AdminSupplierContractController extends BaseController {
  @Inject()
  supplierContractService: SupplierContractService;
}
